package com.example.iqiyitest.utils;

import java.io.File;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.iqiyitest.info.ResultDingdanInfo;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.util.Log;
import okhttp3.Response;

public class HttpClient {
	
	public static Gson gson = new Gson();
	
	public static <T> void post(String url, final CallBack cb) {
		send(HttpMethod.POST,url, null, cb);
	}
	
	public static <T> void post(String url,Map<String, String> paraMap, final CallBack cb) {
		send(HttpMethod.POST,url, paraMap, cb);
	}
	
	public static <T> void get(String url, final CallBack cb) {
		send(HttpMethod.GET,url, null, cb);
	}
	
	public static <T> void get(String url,Map<String, String> paraMap, final CallBack cb) {
		send(HttpMethod.GET,url, paraMap, cb);
	}

	private static void send(HttpMethod method,String url,Map<String, String> paraMap,final CallBack cb) {
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.configCurrentHttpCacheExpiry(1000 * 5);
		RequestCallBack<String> callBack = new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> paramResponseInfo) {
				String result = paramResponseInfo.result;
				Log.e("iqiyitestresult11111111", result);
				if (result != null && !"".equals(result)) {
					ResultDingdanInfo res = null;
					JSONObject jsonObject;
					try {
						jsonObject = new JSONObject(result.toString());
						if (jsonObject.optString("result").equals("false")) {
							cb.onFailure(result.toString());
							return;
						} 
					} catch (JSONException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
						res = gson.fromJson(result, ResultDingdanInfo.class);
					} catch (Exception e) {
						e.printStackTrace();
					}
					if (cb != null && res != null) {
						if (res.getResult().equals("true")) {
							cb.onSuccess(result.toString());
							return;
						} else {
							cb.onFailure(res.toString());
							return;
						}
					}
				}
				if (cb != null)
					cb.onFailure("数据接口未返回任何信息");
			}
			@Override
			public void onFailure(HttpException paramHttpException, String paramString) {
				if (cb != null)
					cb.onFailure(paramString);
			}
		};
		RequestParams para = new RequestParams();
		if(paraMap!=null&&!paraMap.isEmpty()){
			for (String key : paraMap.keySet()) {
				para.addBodyParameter(key, paraMap.get(key));
			}
		}
		httpUtils.send(method, url,para , callBack);
	}
	
	public static void download(String url,String target,final CallBack callBack){
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.download(url, target, new RequestCallBack<File>() {
			
			@Override
			public void onSuccess(ResponseInfo<File> arg0) {
				File file = arg0.result;
				callBack.onSuccess(file.getAbsolutePath());
			}
			
			@Override
			public void onFailure(HttpException arg0, String arg1) {
			}
		});
	}

	public interface CallBack {
		void onSuccess(String str);
		void onFailure(String msg);
	}
}
